package com.bjesc_b2c.app.appraiset.mvp.model.impl;

import android.content.Context;

import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.mvp.model.LoginModel;
import com.rxretrofit_build.util.MapUtil;

import java.util.HashMap;

import master_bean.LoginBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

/**
 * Created by lenovo on 2017/2/27.
 */

public class LoginModelImpl implements LoginModel {
    private CallBack callBack;

    public LoginModelImpl(CallBack callBack) {
        this.callBack = callBack;
    }

    @Override
    public void requestLogin(Context context,String userName, String password) {
        HashMap<String, Object> build = new MapUtil()
                .put("phone", userName)
                .put("pwd", password)
                .build();
        RxRequest.request(ShowType.DISMISS, RxRequest.getClient(context, build).create(IApiActivityRequest.class).login(),
               new BaseSubscriber<TokenBean<LoginBean>>(context) {
                    @Override
                    public void onSuccess(TokenBean<LoginBean> o) {
                        if(callBack!=null && o.getStatus_code() == 0){
                            callBack.callback(o.getData());
                        }
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        if(callBack!=null){
                            callBack.loadFailure();
                        }
                    }
                   @Override
                   public void onStart() {
                       if(callBack!=null){
                           callBack.loadStart();
                       }
                   }
                   @Override
                   public void onCompleted() {
                       if(callBack!=null){
                           callBack. loadCompleted();
                       }
                   }
               });
    }

    public interface CallBack{
        void callback(LoginBean bean);
        void loadStart();
        void loadCompleted();
        void loadFailure();
    }


}

